package com.imooc.hadoop.mapreduce.zip;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @program: hadoop_db
 * @description: WordsCountMapper
 * @author: zengchen
 * @create: 2021-03-28 19:10
 **/
public class ZipWcMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

    Logger logger = LoggerFactory.getLogger(ZipWcMapper.class);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        logger.info("key1 = {}, value1 = {}", key.get(), value);
        if(value != null && StringUtils.isNoneBlank(value.toString())){
            String[] words = value.toString().split(" ");
            for (String word : words) {
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }
}
